<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Docker 镜像参数调试工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input,
        textarea {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            box-sizing: border-box;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        #result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <h1>Docker 镜像参数调试工具</h1>

    <h2>构建镜像</h2>
    <label for="dockerfilePath">Dockerfile 路径:</label>
    <input type="text" id="dockerfilePath" placeholder="例如: ./my-docker-dir">

    <label for="imageTag">镜像标签:</label>
    <input type="text" id="imageTag" placeholder="例如: my-image:1.0">

    <label for="buildArgs">构建参数 (以逗号分隔的键值对, 如 key1=value1,key2=value2):</label>
    <input type="text" id="buildArgs" placeholder="例如: ARG1=value1,ARG2=value2">

    <button onclick="buildImage()">构建镜像</button>

    <h2>运行容器</h2>
    <label for="imageToRun">要运行的镜像:</label>
    <input type="text" id="imageToRun" placeholder="例如: my-image:1.0">

    <label for="containerCmd">容器命令:</label>
    <input type="text" id="containerCmd" placeholder="例如: python script.py">

    <label for="envVars">环境变量 (以逗号分隔的键值对, 如 key1=value1,key2=value2):</label>
    <input type="text" id="envVars" placeholder="例如: VAR1=value1,VAR2=value2">

    <button onclick="runContainer()">运行容器</button>

    <div id="result"></div>

    <script>
        async function buildImage() {
            const dockerfilePath = document.getElementById('dockerfilePath').value;
            const imageTag = document.getElementById('imageTag').value;
            const buildArgs = document.getElementById('buildArgs').value;

            try {
                const response = await fetch('/build', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        dockerfilePath,
                        imageTag,
                        buildArgs
                    })
                });

                const data = await response.json();
                document.getElementById('result').innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
            } catch (error) {
                document.getElementById('result').innerHTML = `<pre>错误: ${error.message}</pre>`;
            }
        }

        async function runContainer() {
            const imageToRun = document.getElementById('imageToRun').value;
            const containerCmd = document.getElementById('containerCmd').value;
            const envVars = document.getElementById('envVars').value;

            try {
                const response = await fetch('/run', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        imageToRun,
                        containerCmd,
                        envVars
                    })
                });

                const data = await response.json();
                document.getElementById('result').innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
            } catch (error) {
                document.getElementById('result').innerHTML = `<pre>错误: ${error.message}</pre>`;
            }
        }
    </script>
</body>

</html>